//
//  NoticeDetailViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/10/31.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "NoticeDetailViewController.h"
#import "UIColor+Hex.h"
#import "AFHTTPRequestOperationManager.h"
#import "MBProgressHUD+MJ.h"
#import "HttpReauestParam.h"
#import "UserDefaultUtil.h"
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height//获取屏幕高度，兼容性测试
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width//获取屏幕宽度，兼容性测试
@interface NoticeDetailViewController ()

@end

@implementation NoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back_click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [self getData];
    [self sendData];
    [super viewDidAppear:animated];
}

-(void) getData{
    
    NSString *userId =[UserDefaultUtil getUserId];
    NSDictionary * dic = @{@"identifier": userId ,@"id":[NSString stringWithFormat:@"%@",_noticeId]};
    
    
    HttpReauestParam * param = [[HttpReauestParam alloc]initWithData:@"app_AppMessage_Details" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    // [MBProgressHUD showMessage:@"获取订单"];
    [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
        
        
        if ([data[@"success"] boolValue]) {
            
            
            [self setNoticeView:data[@"result"]];
            
        }else{
            [MBProgressHUD showError:data[@"errorMsg"]];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"网络连接失败"];
    }];
}

-(void) sendData{
    
    NSString *userId =[UserDefaultUtil getUserId];
    NSDictionary * dic = @{@"identifier": userId ,@"id":[NSString stringWithFormat:@"%@",_noticeId],@"type":@"0"};
    
    
    HttpReauestParam * param = [[HttpReauestParam alloc]initWithData:@"app_AppMessage_Update_Status" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    // [MBProgressHUD showMessage:@"获取订单"];
    [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
        
        
        if ([data[@"success"] boolValue]) {
            
            NSLog(@"success");
            
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"网络连接失败"];
    }];
}


-(void)setNoticeView:(NSDictionary *)dic{
    if ([dic[@"type"] integerValue] == 1) {
        //web页面
        UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, _scrollView.frame.size.height)];
        [webView setUserInteractionEnabled:YES];//是否支持交互
        //[webView setDelegate:self];
        //webView.delegate=self;
        [webView setOpaque:NO];//opaque是不透明的意思
        [webView setScalesPageToFit:YES];//自动缩放以适应屏幕
        [self.scrollView addSubview:webView];
        
        //加载网页的方式
        //1.创建并加载远程网页
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
        
    }else{
        int height = 0;
        UILabel *lab_title = [[UILabel alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 45)];
        lab_title.textAlignment = NSTextAlignmentCenter;
        lab_title.font = [UIFont systemFontOfSize:16];
        lab_title.text = dic[@"title"];
        
        [_scrollView addSubview:lab_title];
        
        height = 45 + height;
        
        UILabel *lab_time = [[UILabel alloc]initWithFrame:CGRectMake(0, height, ScreenWidth, 30)];
        lab_time.textAlignment = NSTextAlignmentCenter;
        lab_time.font = [UIFont systemFontOfSize:10];
        lab_time.text = dic[@"time"];
        
        [_scrollView addSubview:lab_time];
        
        height = 30 + height;
        
        //内容
        UILabel *lab_content = [[UILabel alloc]init];
        
        NSString *content = dic[@"content"];
 
        lab_content.font = [UIFont systemFontOfSize:13];
        lab_content.text = content;
        
        lab_content.numberOfLines = 0;
        lab_content.lineBreakMode = NSLineBreakByTruncatingTail;
        CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
        //关键语句
        CGSize expectSize = [lab_content sizeThatFits:maximumLabelSize];
        lab_content.frame = CGRectMake(20, height, ScreenWidth - 40, expectSize.height);
        
        //设置行间距
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [content length])];
        lab_content.attributedText = attributedString;
        
        
        [_scrollView addSubview:lab_content];
        
        height = expectSize.height + height;
        
        _scrollView.contentSize = _scrollView.frame.size;
    }
}
-(NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
@end
